package cn.shiwei.hr.web.controller;

import cn.shiwei.hr.dto.CourseDto;
import cn.shiwei.hr.service.ICourseService;
import cn.shiwei.hr.domain.Course;
import cn.shiwei.hr.query.CourseQuery;
import cn.shiwei.hr.repository.AjaxResult;
import cn.shiwei.hr.repository.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;

//    /**
//    * 保存和修改
//    */
//    @RequestMapping(value="/save",method= RequestMethod.POST)
//    public AjaxResult save(@RequestBody Course course){
//        if(course.getId()!=null){
//            courseService.updateById(course);
//        }else{
//            courseService.insert(course);
//        }
//        return AjaxResult.me();
//    }

    /**
     * 保存和修改
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseDto courseDto){
        if(courseDto.getCourse().getId()!=null){
            courseService.updateByIdToDto(courseDto);
        }else{
            courseService.insertToDto(courseDto);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return AjaxResult.me();
    }

    /**
    * 根据ID获取对象
    */
    @PreAuthorize("hasAuthority('course:get')") // 开启权限校验
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(courseService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @PreAuthorize("hasAuthority('course:list')") // 开启权限校验
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(courseService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @PreAuthorize("hasAuthority('course:pagelist')") // 开启权限校验
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        PageList pageList = new PageList<Course>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    /**
     * 课程上架
     * onLineCours    */
    @PostMapping("/onLineCourse/{id}")
    public AjaxResult onLineCourse(@PathVariable Long id){
        courseService.onLineCourse(id);
        return AjaxResult.me();
    }

    /**
     * 课程下架
     * onLineCours    */

    @PostMapping("/offLineCourse/{id}")
    @PreAuthorize("hasAuthority('course:offLine')") // 开启权限校验
    public AjaxResult offLineCourse(@PathVariable Long id){
        courseService.offLineCourse(id);
        return AjaxResult.me();
    }
}
